package com.qianniu.napi.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Pyq;
import com.qianniu.napi.admin.mapper.PyqMapper;
import com.qianniu.napi.admin.service.IPyqService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2018-12-17
 */
@Service
public class PyqServiceImpl extends ServiceImpl<PyqMapper, Pyq> implements IPyqService {

    private static final Logger logger = LoggerFactory.getLogger(PyqServiceImpl.class);

    @Autowired
    private PyqMapper pyqMapper;

    public List<Pyq> getHyList(Integer pageIndex, Integer pageSize, Double lat, Double lng, Long mpid, Integer type, String orderby) {
        List<Pyq> list = new ArrayList<Pyq>();
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("mpid", mpid);
//        if(!Strings.isNullOrEmpty(shicode)) map.put("shicode", shicode);
        if (type != null && type > 0) map.put("type", type);

        logger.debug("====================getYxgoodsList map=" + JSON.toJSONString(map));

        int total = pyqMapper.getHypyqCount(map);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(orderby)) orderby = "distance";

        logger.debug("==========getYxgoodsList 111");

        map.put("lat", lat);
        map.put("lng", lng);
        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("orderby", orderby);
        String sort = "desc";
        if (!Strings.isNullOrEmpty(orderby) && !orderby.equals("distance")) {
            sort = "asc";
        }
        map.put("sort", sort);

        logger.debug("==========getYxgoodsList map=" + JSON.toJSONString(map));
        list = pyqMapper.getHypyqList(map);

        return list;
    }
}
